// pages/student/period/addnewsign.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      newtime:'',
      time:null,
      id:'',
      list:'',
      page:0,
    liats:[],
  },

  // 点击打卡
  tapclick(){
    var that = this;
    wx.showLoading({
      title: '打卡中~',
    })
    wx.request({
      url: CONFIG.API_HOST + 'crm/Courseregist/sign',
      data: {
        course_regist_id: that.data.id,
      },
      header: {
        'ContentType': 'application/json'
      },
      success: function (res) {
        // console.log(res);
        if (res.data.status == 1001 || res.data.status == '1001') {
          that.setData({
            page:0
          })
          that.signLog();
          wx.showToast({
            title: '打卡成功',
            icon: 'none',
            duration: 2000,
          })
          
          wx.hideLoading();
        }else{
          wx.hideLoading();
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000,
          })
        }
        
      }
    })
    // wx.getSetting({
    //   success: function (res1) {
    //     console.log(res1);
    //     wx.getLocation({
    //       success: res => {
    //         console.log(res);
    //         // this.setData({
    //         //   location: res,
    //         // })
    //         // console.log(app.globalData.location);
    //       },
    //     })
    //   }
    // })

  },

  // 获取打卡记录
  signLog(){
    var that = this;
    wx.request({
      url: CONFIG.API_HOST + 'crm/Courseregist/signLog',
      data: {
        course_regist_id: that.data.id,
        page:that.data.page,
      },
      header: {
        'ContentType': 'application/json'
      },
      success: function (res) {
        // console.log(res);
        if (res.data.status == 1001 || res.data.status == '1001') {
          var data = res.data;
          if(Number(that.data.page)==0){
            that.setData({
              list: data.data,
              liats:data.data.lists
            })
          }else{
            var lists = that.data.liats.concat(data.data.lists);
            that.setData({
              list: data.data,
              liats: lists
            })
          }
          // console.log(data);
          
          // console.log(that.data.list);
          wx.hideLoading();
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000,
          })
        }

      }
    })
  },

  formatTime(date) {
    var that = this;
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
 
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
 
    var time = [year, month, day].map(that.formatNumber).join('-') + ' ' + [hour, minute, second].map(that.formatNumber).join(':');
    wx.hideLoading()
    // console.log(time);
    that.setData({
      newtime:time
    })
  },
 
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      wx.showLoading({
        title: '加载中~',
      })
    var id = options.id;
    that.setData({
      id:id,
    })
      var time = setInterval(function(){
        that.formatTime(new Date());
      },1000)
      that.setData({
        time:time
      })

    that.signLog();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.time);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.time);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '加载中~',
    })
    var page = that.data.page + 1;
    // console.log(page);
    that.setData({
      page:page
    })
    that.signLog();
  },
})